// v2010.09.21

/*
Copyright (c) 2010 Ben Hendel-Doying

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package world;

import game.ASCII;

import java.awt.Graphics;

abstract public class Equipment extends Item
{
	public static final int DEFAULT_DURABILITY = 100;

	protected Attributes attributes;

	protected int durability_current, durability_max;

	public Equipment(Equipment t)
	{
		super(t);

		attributes = new Attributes(t.attributes);

		durability_max = DEFAULT_DURABILITY;
		durability_current = DEFAULT_DURABILITY;
	}

	public Equipment(int l)
	{
		super();

		attributes = new Attributes();

		durability_max = DEFAULT_DURABILITY;
		durability_current = DEFAULT_DURABILITY;

		while(l > 0)
		{
			switch(World.RNG.nextInt(13))
			{
				case 0: attributes.attack++; l--; value += 20; break;
				case 1: attributes.gehenna++; l--; value += 10; break;
				case 2: attributes.block++; l--; value += 20; break;
				case 3: attributes.damage_dark++; l -= 2; value += 50; break;
				case 4: attributes.damage_fire++; l -= 2; value += 45; break;
				case 5: attributes.damage_light++; l -= 2; value += 50; break;
				case 6: attributes.dodge++; l--; value += 20; break;
				case 7: attributes.heal++; l--; value += 15; break;
				case 8: attributes.HP_maximum++; l--; value += 20; break;
				case 9: attributes.pray++; l--; value += 15; break;
				case 10: attributes.resist_dark++; l -= 2; value += 40; break;
				case 11: attributes.resist_fire++; l -= 2; value += 35; break;
				case 12: attributes.resist_light++; l -= 2; value += 40; break;
			}
		}
	}

	public String GetName()
	{
		if(durability_current == 0)
			return super.GetName() + " (broken)";
		else
			return super.GetName();
	}

	public boolean Damage(int d)
	{
		if(d >= durability_current)
		{
			durability_current = 0;
			durability_max = 0;

			return true;
		}
		else
		{
			durability_current -= d;

			return false;
		}
	}

	public boolean Mend()
	{
		if(durability_current < durability_max)
		{
			durability_current = durability_max;
			return true;
		}
		else
			return false;
	}

	final public int GetAttack() { return attributes.attack; }
	final public int GetBlock() { return attributes.block; }
	final public int GetDodge() { return attributes.dodge; }

	final public int GetHeal() { return attributes.heal; }
	final public int GetPray() { return attributes.pray; }
	final public int GetBless() { return attributes.bless; }
	final public int GetGehenna() { return attributes.gehenna; }

	final public int GetFireDamage() { return attributes.damage_fire; }
	final public int GetLightDamage() { return attributes.damage_light; }
	final public int GetDarkDamage() { return attributes.damage_dark; }

	final public int GetFireResistance() { return attributes.resist_fire; }
	final public int GetLightResistance() { return attributes.resist_light; }
	final public int GetDarkResistance() { return attributes.resist_dark; }

	final public int GetMaxHP() { return attributes.HP_maximum; }

	public void RenderDescription(Graphics g, int color, int x, int y)
	{
		ASCII.DrawString(g, "Equip to gain the following bonuses:", color, x, y);

		if(GetAttack() > 0)
			ASCII.DrawString(g, "+" + GetAttack() + " Attack", color, x, ++y);
		if(GetBlock() > 0)
			ASCII.DrawString(g, "+" + GetBlock() + " Block", color, x, ++y);
		if(GetDodge() > 0)
			ASCII.DrawString(g, "+" + GetDodge() + " Dodge", color, x, ++y);
		if(GetHeal() > 0)
			ASCII.DrawString(g, "+" + GetHeal() + " Heal", color, x, ++y);
		if(GetPray() > 0)
			ASCII.DrawString(g, "+" + GetPray() + " Pray", color, x, ++y);
		if(GetBless() > 0)
			ASCII.DrawString(g, "+" + GetBless() + " Bless", color, x, ++y);
		if(GetGehenna() > 0)
			ASCII.DrawString(g, "+" + GetGehenna() + " Gehenna", color, x, ++y);
		if(GetFireDamage() > 0)
			ASCII.DrawString(g, "+" + GetFireDamage() + " Fire damage", color, x, ++y);
		if(GetLightDamage() > 0)
			ASCII.DrawString(g, "+" + GetLightDamage() + " Light damage", color, x, ++y);
		if(GetDarkDamage() > 0)
			ASCII.DrawString(g, "+" + GetDarkDamage() + " Dark damage", color, x, ++y);
		if(GetFireResistance() > 0)
			ASCII.DrawString(g, "+" + GetFireResistance() + " Fire resistance", color, x, ++y);
		if(GetLightResistance() > 0)
			ASCII.DrawString(g, "+" + GetLightResistance() + " Light resistance", color, x, ++y);
		if(GetDarkResistance() > 0)
			ASCII.DrawString(g, "+" + GetDarkResistance() + " Dark resistance", color, x, ++y);
		if(GetMaxHP() > 0)
			ASCII.DrawString(g, "+" + GetMaxHP() + " maximum HP", color, x, ++y);

		ASCII.DrawString(g, "Durability: " + durability_current + "/" + durability_max, color, x, y + 2);
	}
}
